package main

import (
	"fmt"
	"sync"
	"time"
)

// 计算1到20的每个数的阶乘的和
// 定义全局变量
var (
	nums map[int]int = make(map[int]int, 10)
	lock             = sync.Mutex{}
)

func test(num int) {
	var temp int = 1
	for i := 1; i <= num; i++ {
		temp *= i
	}
	lock.Lock()
	nums[num] = temp
	lock.Unlock()
}

func main() {

	//fmt.Println()
	for i := 1; i <= 20; i++ {
		//定义协程
		go test(i)
	}
	time.Sleep(time.Second * 5)
	lock.Lock()
	for _, num := range nums {
		fmt.Println(num)
	}
	lock.Unlock()
}
